<template>
  <div class="search">
    <div class="search-ss">
      <van-icon name="arrow-left" @click="goHome" />
      <form action="">
        <van-search v-model="values" placeholder="请输入搜索关键词" />
      </form>
      <van-button type="default" @click="goSearch">搜索</van-button>
    </div>
    <div class="sort">
      <ul>
        <li>综合</li>
        <li>销量</li>
        <li>新品</li>
        <li>价格<van-icon name="arrow-down" /></li>
      </ul>
    </div>
    <div class="goods">
      <ul>
        <li v-for="item in searchList" :key="item.id">
          <img :src="item.imgSrc" alt="" />
          <div class="title">{{ item.title }}</div>
          <div class="price">
            <span>${{ item.spanPrice }}</span>
            <span>{{ item.hot }}<van-icon name="star-o" /></span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script setup>
import { useRouter, useRoute } from "vue-router";
import { ref, onMounted } from "vue";
import { getSearchIndexApi } from "@/utils/api/api.js";

// 跳转首页
const router = useRouter();
const goHome = () => {
  router.push("/");
};
// 获取搜索页初始商品数据
const route = useRoute();
const values = route.query.key || "";
const searchList = ref([]);

const getGoodsList = async () => {
  await getSearchIndexApi().then(({ data }) => {
    searchList.value = data.data;
  });
};

onMounted(() => {
  getGoodsList();
});
</script>
<style lang="less" scoped>
.search {
  // overflow: hidden;
  position: absolute;
  top: 0;
  width: 100%;
  // background-color: #fff;
  height: 100%;
  .search-ss {
    display: flex;
    align-items: center;
    width: 100%;
    .van-icon {
      background-color: #fff;
      font-size: 20px;
      height: 54px;
      line-height: 54px;
      flex: 1;
    }
    .van-button {
      padding: 0;
      font-size: 16px;
      border: #fff;
      border: 0px;
      height: 54px;
      flex: 1;
    }
    .van-search {
      flex-shrink: 0;
      width: 260px;
      border: 0px #fff;
    }
  }
  .sort {
    ul {
      background-color: #fff;
      display: flex;
      justify-content: space-around;
      border-top: 1px solid #8a8686;
      border-bottom: 1px solid #8a8686;
      li {
        margin: 5px 10px;
        padding: 0 20px;
        font-size: 18px;
      }
    }
  }
}
.goods {
  ul {
    display: flex;
    justify-content: space-between;
    padding: 10px 10px;
    background-color: #fff;
    flex-wrap: wrap;
    li {
      width: 45%;
      margin: 10px 5px;
      img {
        width: 85%;
        height: 260px;
      }
      .price {
        display: flex;
        justify-content: space-between;
        span {
          font-size: 16px;
        }
      }
    }
  }
}
.title {
  font-size: 16px;
  text-align: left;
}
</style>
